<html>
<head><meta charset="utf-8"><title>using ra as a library · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/using.20ra.20as.20a.20library.html">using ra as a library</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="191340917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/using%20ra%20as%20a%20library/near/191340917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tcjqlwnsqthqcakslc <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/using.20ra.20as.20a.20library.html#191340917">(Mar 21 2020 at 11:17)</a>:</h4>
<p>Hi everyone, is it possible to get a list of all symbols including their type?</p>
<p>This is what I currently have, but I get empty lists.</p>
<div class="codehilite"><pre><span></span>const LOCAL_CODE: SourceRootId = SourceRootId(0);
const SYSROOT: SourceRootId = SourceRootId(1);

struct PackageRootWrapper(PackageRoot);

impl Debug for PackageRootWrapper {
    fn fmt(&amp;self, f: &amp;mut Formatter&lt;&#39;_&gt;) -&gt; Result&lt;(), Error&gt; {
        f.debug_struct(&quot;PackageRoot&quot;)
            .field(&quot;path&quot;, self.0.path())
            .field(&quot;is_member&quot;, &amp;self.0.is_member())
            .finish()
    }
}

fn main() -&gt; anyhow::Result&lt;()&gt; {
    let mut analysis_host = AnalysisHost::default();
    let mut change = AnalysisChange::new();

    let workspace = ProjectWorkspace::discover_with_sysroot(
        Path::new(&quot;/home/user/rust/analyzer-test&quot;),
        true,
        &amp;CargoFeatures::default(),
    )?;
    dbg!(workspace
        .to_roots()
        .iter()
        .map(|pr| PackageRootWrapper(pr.clone()))
        .collect::&lt;Vec&lt;_&gt;&gt;());

    for (i, pr) in workspace.to_roots().iter().enumerate() {
        change.add_root(SourceRootId(i as u32), pr.is_member());
        change.set_debug_root_path(
            SourceRootId(i as u32),
            pr.path().to_str().unwrap().to_string(),
        );
    }

    let mut next_file_id = 0u32;
    let mut file_id_map: FxHashMap&lt;PathBuf, FileId&gt; = FxHashMap::default();
    let mut get_or_insert_file_id = |p: &amp;Path| -&gt; Option&lt;FileId&gt; {
        let p = p.to_path_buf();
        match file_id_map.get(&amp;p) {
            Some(id) =&gt; Some(*id),
            None =&gt; {
                next_file_id += 1;
                file_id_map.insert(p, FileId(next_file_id));
                Some(FileId(next_file_id))
            }
        }
    };

    change.add_root(LOCAL_CODE, true);
    change.add_file(
        LOCAL_CODE,
        FileId(0),
        RelativePathBuf::from_path(&quot;src/main.rs&quot;).unwrap(),
        Arc::new(&quot;fn main() {}&quot;.to_string()),
    );
    let crate_graph = workspace.to_crate_graph(
        &amp;CfgOptions::default(),
        &amp;FxHashMap::default(),
        &amp;mut get_or_insert_file_id,
    );
    change.set_crate_graph(crate_graph);

    analysis_host.apply_change(change);
    let mut query = Query::new(&quot;to&quot;.to_string());
    query.libs();
    let db: &amp;mut RootDatabase =
        Any::downcast_mut::&lt;RootDatabase&gt;(analysis_host.raw_database_mut()).unwrap();
    dbg!(symbol_index::world_symbols(db, query));
    dbg!(db.library_roots());
    dbg!(db.local_roots());
    dbg!(db.library_symbols(SourceRootId(0)));
    // dbg!(an.symbol_search(query));
    Ok(())
}
</pre></div>



<a name="191340986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/using%20ra%20as%20a%20library/near/191340986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/using.20ra.20as.20a.20library.html#191340986">(Mar 21 2020 at 11:19)</a>:</h4>
<p>Take a look at <a href="https://github.com/rust-analyzer/rust-analyzer/blob/10867336e627f84a4886592c0a2764f5105bd0ce/crates/rust-analyzer/src/cli/analysis_stats.rs#L75-L77" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/10867336e627f84a4886592c0a2764f5105bd0ce/crates/rust-analyzer/src/cli/analysis_stats.rs#L75-L77">https://github.com/rust-analyzer/rust-analyzer/blob/10867336e627f84a4886592c0a2764f5105bd0ce/crates/rust-analyzer/src/cli/analysis_stats.rs#L75-L77</a>, which collects all <em>functions</em></p>



<a name="191341800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/using%20ra%20as%20a%20library/near/191341800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tcjqlwnsqthqcakslc <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/using.20ra.20as.20a.20library.html#191341800">(Mar 21 2020 at 11:42)</a>:</h4>
<p>thanks, I'll look into that</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>